package com.bs.bszhsq.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.bszhsq.entity.Dictionary;
import com.bs.bszhsq.entity.Role;
import com.bs.bszhsq.mapper.DictionaryMapper;
import com.bs.bszhsq.utils.MessageType;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import com.bs.bszhsq.utils.R;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import com.bs.bszhsq.service.IMenuService;
import com.bs.bszhsq.entity.Menu;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 章宇光
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理接口")
        public class MenuController {
    //service注入
@Resource
private IMenuService menuService;
@Resource
private DictionaryMapper dictionaryMapper;


@PostMapping
@ApiOperation(value = "新增")
public R save(@RequestBody Menu menu){
        boolean save= menuService.save(menu);
        return R.write(save);
        }


@PostMapping("/delete")
@ApiOperation(value = "删除")
public R delete(@RequestBody Long id){
        boolean b=menuService.removeById(id);
        return R.write(b);
        }

@PostMapping("/update")
@ApiOperation(value = "修改")
public R update(@RequestBody Menu menu) {
        boolean update = menuService.updateById(menu);
        return R.write(update);
        }


@PostMapping("/deletebatch")
@ApiOperation(value = "批量删除")
public R deletebatch(@RequestBody List<Long> ids) {
        boolean b = menuService.removeByIds(ids);
        return R.write(b);
        }


@GetMapping("/page")
@ApiOperation(value = "分页查询")
public R page(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam(defaultValue = "") String name) {
        IPage<Menu> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
                qw.like("name", name);
        }
        qw.orderByDesc("id");
        IPage<Menu> page1 = menuService.page(page, qw);
        return R.success(page1);
        }


@GetMapping("/all")
@ApiOperation(value = "查询")
public R getAll(@RequestParam(defaultValue = "") String name){
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        if (!"".equals(name)) {
                qw.like("name", name);
        }
        List<Menu> list = menuService.list(qw);
        List<Menu> parentNode = list.stream().filter(menu -> menu.getPid()== 0).collect(Collectors.toList());
        for (Menu menu : parentNode) {
                List<Menu> collect = list.stream().filter(menu1 -> menu.getId().equals(menu1.getPid())).collect(Collectors.toList());
                menu.setChildren(collect);
        }

        return R.success(parentNode);
}



@GetMapping("/icons")
@ApiOperation(value = "图标")
public R icons(){
        LambdaQueryWrapper<Dictionary> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Dictionary::getType, MessageType.DICTIONARY_ICON.getCode());
        List<Dictionary> dictionaries = dictionaryMapper.selectList(lqw);
        return R.success(dictionaries);
}



        }
